package v1ch06.lambda;

import javax.swing.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 该程序演示了lambda表达式的用法。
 *
 * @author Cay Horstmann
 * @version 1.0 2015-05-12
 */
public class LambdaTest {
	public static void main(String[] args) {
		//test1();
		test2();
	}
	
	private static void test1() {
		String[] planets = new String[]{"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune"};
		System.out.println(Arrays.toString(planets));
		//1
		Arrays.sort(planets);
		System.out.println("按字典顺序排序:" + Arrays.toString(planets));
		//2
		Arrays.sort(planets, (first, second) -> first.length() - second.length());
		System.out.println("按长度排序:" + Arrays.toString(planets));
		//3
		Arrays.sort(planets, String::compareToIgnoreCase);
		System.out.println("方法引用排序" + Arrays.toString(planets));
		
		Timer t = new Timer(1000, event -> System.out.println("The time is " + new Date()));
		t.start();
		
		// 保持程序运行，直到用户选择“确定”为止
		JOptionPane.showMessageDialog(null, "Quit program?");
		System.exit(0);
	}
	
	/**
	 * stream去重、赛选、排序、跳过、限制、循环
	 */
	private static void test2() {
		List<String> list = new ArrayList<>();
		list.add("张无忌");
		list.add("周芷若");
		list.add("赵敏");
		list.add("张强");
		list.add("张三丰");
		list.add("张三丰");
		list.add("王丽云");
		list.add("张一民");
		list.add("张一民");
		list.add("张一民");
		list.add("张一民");
		list.add("张一民");
		list.add("张一民");
		list.add("张一民");
		list.add("张风雪");
		list.stream()
		    .distinct()
		    .filter(ss -> ss.startsWith("张"))
		    .filter(ss -> ss.length() >= 2)
		    .sorted((first, second) -> first.length() - second.length())
		    .skip(1)
		    .limit(3)
		    .forEach(System.out::println);
	}
}
